<?php
include("common/common.php");

function startWith($str, $needle) {

	return strpos($str, $needle) === 0;

}

$date = $_POST["date"];
$hour = $_POST["hour"];
$minute = $_POST["minute"];
$peoplenum = $_POST["peoplenum"];
$email=$_POST["uemail"];
$sex=$_POST["usex"];
$nom=$_POST["unom"];
$prenom=$_POST["uprenom"];
$tel=$_POST["utel"];
$descr=stripslashes($_POST["descr"]);

$restoid=$_POST["restoid"];

	$data['orderdate'] = $date;
	$data['numb'] = $peoplenum;
	$data['minutes'] = $minute;
	$data['hours'] = $hour;
	$data['ordertype'] = 1;
	$data['email'] = $email;
	$data['sex'] = $sex;
	$data['nom'] = $nom;
	$data['prenom'] = $prenom;
	$data['tel'] = $tel;
	$data['descr'] = $descr;
	$data['restoid'] = $restoid;
	$data['createtime'] = date("Y-m-d H:i:s");
	$data['status'] = "ok";
	$db = Database::obtain(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
	$db->connect();
	 
	$sql = "select * from info_resto where RESTO_ID=".$restoid;
	$restoinfo = $db->query_first($sql);
	$sql2 = "SELECT email FROM `website`";
	$sitemail = $db->query_first($sql2);
	
	$primary_id = $db->insert("order_info", $data);
	$orderid =  $primary_id+28904;
	$updata['orderid'] = $orderid;
	$db->update("order_info", $updata,"id=".$primary_id);
	$db->close();
	
	//send order mail
	$host = 'http://'.$_SERVER['HTTP_HOST'];
	$root_path=get_root_path();
	$pic_path = null;
	if(startWith($str, '/')){
		$pic_path = $host.$restoinfo['INTRO_PIC'];
	}else{
		$pic_path = $root_path.$restoinfo['INTRO_PIC'];
	}
	$_lang = null;
	if (is_null($_lang) and isset($_COOKIE['_lang'])){
		$_lang=$_COOKIE['_lang'];
	}else{
		if(is_null($_lang)){
			$_lang = FR;
		}
	}
    $message ='
	<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<title>kacho-resto</title>
	</head>
	<body>
	<div style="background:url(http://kacho-resto.com/mail/images/mail-bg.png);width:650px;overflow:hidden;position:relative;height:500px">
	<div style="width:650px; background:url(http://kacho-resto.com/mail/images/headBg.png) repeat-x; height:147px; position:absolute; z-index:10">
	<div style="height:147px">
	<h1 style="background:url(http://kacho-resto.com/mail/images/logo.png) no-repeat;width:191px; height:68px; margin:30px 0 0 20px;float:left"></h1>
		
	';
	if($_lang==FR){
		$message .='
		<div style="width:400px; height:45px; overflow:hidden; left:225px; top:45px; position:absolute; font-size:18px; color:#AEAEAE;font-family:Lucida Handwriting"></div>
		<div style="width:400px; height:20px; overflow:hidden; left:225px; position:absolute; font-size:18px; color:#AEAEAE; top:55px;font-family:Lucida Handwriting">';
		$message .= "
		Votre 1er portail franco-arabe de restauration méditerranéenne
		";
	}else{
		$message .='
		<div style="width:400px; height:45px; overflow:hidden; right:25px; top:45px; position:absolute; font-size:18px; color:#AEAEAE;font-family:Lucida Handwriting"></div>
		<div style="width:400px; height:20px; overflow:hidden; text-align:right; right:25px; position:absolute; font-size:18px; color:#AEAEAE; top:55px;font-family:Lucida Handwriting">';
		$message .= "
		موقعكم الوحيد باللغة العربية لعالم المأكولات والضيافة المغربية والشرق أوسطية
		";
	}
	 
	$message .='
	</div>
	</div>
	</div>
	<!--header end of-->
	';
	$message .='<div style="width:650px; height:500px;filter:progid:DXImageTransform.Microsoft.gradient(startcolorstr=#99ffffff,endcolorstr=#99ffffff); position:absolute; top:147px">
	<h3 style="padding:10px 10px 0 20px;font-size:13px;color:#000;font-family:Helvetica Neue, regular">';
	if($_lang==FR){
 		$message .='Bonjour ';
 		$message.= $prenom.',</h3>';
     }else{
     	$message.= '،'.$prenom.' ';
     	$message .='مرحباً ';
     	$message.='</h3>';
     }
	//common
	
	if($_lang==FR){
	$message.='
	  <p style="padding:0 0 10px 20px;font-size:13px;color:#000;font-family:Helvetica Neue">Merci pour votre réservation !</p>
   <div style="width:600px; height:60px">
      <table style="margin-left:20px;">
      <tr>
         <td>
             <img src="http://kacho-resto.com/mail/images/mail-confirmation.png" style="width:25px; height:24px;"></img>
         </td>
         <td style="border-right:1px solid #888;width:245px">     
               <p style="margin-bottom:0px;font-size:13px"><b>Réservation confirmée</b></p>
               <p style="margin-top:0px;font-size:13px">Numéro de réservation : '.$orderid.'</p>
         </td>
         <td style="color:#000;width:300px;margin-left:20px;">
          		<p style="margin-bottom:0px;font-size:13px"><b>&nbsp; Le '.$date.' à '.$hour.'h'.$minute.'</b></p>
          		<p style="margin-top:0px;font-size:13px"><b>&nbsp; '.$peoplenum.' personnes</b></p>
         </td>
      </tr>
      </table>
   </div>
   <!--emailOder end of-->
   <div style="width:630px; height:134px; padding:20px 0 0 20px; background-color:#fafafa">
   <table>
   <tr>
     <td style="width:270px">
     <img src="'.$pic_path.'" width="250" height="112" alt=""/>
     </td>
     <td>
       <dt style="font-weight:bold;font-size:13px; margin-bottom:5px; color:#333">'.$restoinfo['RESTO_NAME_FR'].' </dt>
       <dt style="font-size:13px;color:#000;">'.$restoinfo['RESTO_ADDR'].'</dt>
       <dt style="font-size:13px;color:#000;margin-bottom:20px">Tél : '.$restoinfo['RESTO_TEL'].'</dt>
       <dt>
          <code><a style="color:#4c7b00" href="'.$root_path.'diningHallinfr.php?restoid='.$restoid.'&nationid='.$restoinfo['RESTO_NATION'].'&typeid='.$restoinfo['RESTO_TYPE_ID'].'"><i style="background:url(http://kacho-resto.com/mail/images/mail-plan.png) no-repeat; height:27px;width:18px;vertical-align:middle; margin-right:6px; display:inline-block"></i>Voir le plan</a></code>
          <code><a style="color:#4c7b00" href="'.$root_path.'diningHallinfr.php?restoid='.$restoid.'&nationid='.$restoinfo['RESTO_NATION'].'&typeid='.$restoinfo['RESTO_TYPE_ID'].'"><i style="background:url(http://kacho-resto.com/mail/images/mail-info.png) no-repeat; height:17px;width:18px;vertical-align:middle; margin-right:6px; display:inline-block"></i>Plus d’info</a></code>
          <code><a style="color:#4c7b00" href="'.$root_path.'cancelorder.php?id='.$primary_id.'"><i style="background:url(http://kacho-resto.com/mail/images/annuler.png) no-repeat; height:20px;width:18px;vertical-align:middle; margin-right:6px; display:inline-block"></i>Annuler</a></code>
        </dt>
      </td>
     </tr>
     </table>
   </div>
   <!--shop end of-->
   <div  style="width:630px; height:104px; padding:20px 0 0 20px;">
      <dt style="font-weight:bold;font-size:13px; margin-bottom:5px; color:#4c7b00">Remarques :</dt>
      <dt style="font-size:13px;color:#000; margin-bottom:10px;">'.$descr.'</dt>
      <dt style="font-weight:bold;font-size:13px; margin-bottom:5px; color:#4c7b00">Quelques conseils :</dt>
      <dt style="font-size:13px;color:#000; margin-bottom:0px;">Votre table vous attend, inutile d’appeler le restaurant.</dt>
      <dt style="font-size:13px;color:#000; margin-bottom:0px;">Par contre, si vous pensez avoir plus de 15 min de retard, pensez à prévenir le restaurant avant qu’il ne propose votre table à un autre client.</dt>
   </div>
   <!--emailList end of-->
	';
	}else{
		
		$message.='
		<p style="padding:0 0 10px 20px;font-size:13px;color:#000;font-family:Helvetica Neue">! شكراً لحجزكم </p>
		<div style="width:600px; height:60px">
		<table style="margin-left:20px;">
		<tr>
		<td>
		<img src="http://kacho-resto.com/mail/images/mail-confirmation.png" style="width:25px; height:24px;"></img>
		</td>
		<td style="border-right:1px solid #888;width:245px">
		<p style="margin-bottom:0px;font-size:13px"><b>تأكيد الحجز </b></p>
		<p style="margin-top:0px;font-size:13px"> رقم الحجز : '.$orderid.'</p>
		</td>
		<td style="color:#000;width:300px;margin-left:20px;">
		<p style="margin-bottom:0px;font-size:13px"><b>&nbsp; Le '.$date.' à '.$hour.'h'.$minute.'</b></p>
		<p style="margin-top:0px;font-size:13px"><b>&nbsp; '.$peoplenum.' personnes</b></p>
		</td>
		</tr>
		</table>
		</div>
		<!--emailOder end of-->
		<div style="width:630px; height:134px; padding:20px 0 0 20px; background-color:#fafafa">
		<table>
		<tr>
		<td style="width:270px">
		<img src="'.$pic_path.'" width="250" height="112" alt=""/>
		</td>
		<td>
		<dt style="font-weight:bold;font-size:13px; margin-bottom:5px; color:#333">'.$restoinfo['RESTO_NAME_ARB'].' </dt>
		<dt style="font-size:13px;color:#000;">'.$restoinfo['RESTO_ADDR'].'</dt>
		<dt style="font-size:13px;color:#000;margin-bottom:20px">Tél : '.$restoinfo['RESTO_TEL'].'</dt>
		<dt>
		<table>
		<tr>
		<td><code><a style="color:#4c7b00" href="'.$root_path.'diningHallinfr.php?restoid='.$restoid.'&nationid='.$restoinfo['RESTO_NATION'].'&typeid='.$restoinfo['RESTO_TYPE_ID'].'">الخريطة <i style="background:url(http://kacho-resto.com/mail/images/mail-plan.png) no-repeat; height:27px;width:18px;vertical-align:middle; margin-right:6px; display:inline-block"></i></a></code>
		<td><code><a style="color:#4c7b00" href="'.$root_path.'diningHallinfr.php?restoid='.$restoid.'&nationid='.$restoinfo['RESTO_NATION'].'&typeid='.$restoinfo['RESTO_TYPE_ID'].'">زيادة معلومات <i style="background:url(http://kacho-resto.com/mail/images/mail-info.png) no-repeat; height:17px;width:18px;vertical-align:middle; margin-right:6px; display:inline-block"></i></a></code>
		<td><code><a style="color:#4c7b00" href="'.$root_path.'cancelorder.php?id='.$primary_id.'">إلغاء <i style="background:url(http://kacho-resto.com/mail/images/annuler.png) no-repeat; height:20px;width:18px;vertical-align:middle; margin-right:6px; display:inline-block"></i></a></code>
		</tr>
		</table>
		</dt>
		</td>
		</tr>
		</table>
		</div>
		<!--shop end of-->
		<div  style="width:630px; height:104px; padding:20px 0 0 20px;">
		<dt style="font-weight:bold;font-size:13px; margin-bottom:5px; color:#4c7b00">Remarques :</dt>
        <dt style="font-size:13px;color:#000; margin-bottom:10px;">'.$descr.'</dt>
		<dt style="font-weight:bold;font-size:13px; margin-bottom:5px; color:#4c7b00">: بعض النصائح</dt>
		<dt style="font-size:13px;color:#000; margin-bottom:0px;">.نحن في إنتظاركم. في حال ألتأخيرأكثرمن ١٥ دقيقة يرجى ألإتصال بالمطعم كي لا يلغى الحجز</dt>
		</div>
		<!--emailList end of-->
		';
		
		
	}
	//common
	$message .='<ul style="{width:600px; margin:30px auto">
	<li style="height:48px;margin-top:20px;margin-bottom:10px; font-size:12px"><img style="vertical-align:middle; margin-right:20px" src="http://kacho-resto.com/mail/images/logoPic_1.png" width="83" height="48" />';
	if($_lang==FR){
        $message .='A bientôt sur';
        $message .= '<a href="'.$root_path.'" style="color:#333; text-decoration: underline"> kacho-resto.com</a> !</li>';
     }else{
     	$message .= '! <a href="'.$root_path.'" style="color:#333; text-decoration: underline">kacho-resto.com</a> ';
     	$message .='الى اللقاء على';
     	$message .= '</li>';
     }
      
     $message .= '<li style="margin-right:10px;float:right">
         By <a href="'.$root_path.'kac.php?menu=3" style="color:#ff6700; text-decoration: underline">Kachmir Consultancy</a>
     </li>
   </ul>
</div>
<!--regEmail end of-->
</div>
</body>
</html>
';

	$to = $email.','.$sitemail['email'];
	if (isset($restoinfo['RESTO_MAIL'])){
		$to.=','.$restoinfo['RESTO_MAIL'];
	}
	$subject = "www.kacho-resto.com";
	$from = $sitemail['email'];
	sendmail($from, $to, $subject, $message);
	
	
	redirect("reserveok.php?restoid=".$restoid);
?>
